package com.fone.player.play.adapter;

import java.util.List;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.DramaRst;
import com.fone.player.client.DramaRst.Cnt;
import com.fone.player.play.ChangeVideoEvent;
import com.fone.player.play.VideoPlayerMessage;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;

/**
 * 全屏播放页规则剧集Item adapter
 * 
 * @author kongxiaojun
 * @since 2014-5-29
 */
public class FullRuleSeriesItemAdapter extends BaseAdapter {

	private List<DramaRst.Cnt> cntList;
	private String currentPlayUrl;
	private String cId;
	private static final String TAG = "FullRuleSeriesItemAdapter";

	public FullRuleSeriesItemAdapter(List<Cnt> cntList, String playUrl) {
		super();
		this.cntList = cntList;
		this.currentPlayUrl = playUrl;
		this.cId = FoneUtil.getCIdByUrl(playUrl);
	}

	@Override
	public int getCount() {
		if (cntList == null) {
			return 0;
		}
		return cntList.size();

	}

	@Override
	public Object getItem(int position) {
		return cntList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DramaRst.Cnt cnt = cntList.get(position);
		ViewHolder holder = null;

		if (convertView == null) {
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.full_video_rule_series_item, null);
			holder = new ViewHolder((TextView) convertView.findViewById(R.id.video_series_rule_item_name));
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(cnt.name);
		holder.name.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.videofull_series_mode1_item_text_color));
		convertView.setBackgroundResource(R.drawable.full_video_series_normal_shape);
		if (FoneUtil.getCIdByUrl(cnt.url).equals(cId)) {
			convertView.setBackgroundResource(R.drawable.full_video_series_watching_shape);
			holder.name.setTextColor(Color.WHITE);
		}
		convertView.setTag(holder);
		return convertView;
	}

	class ViewHolder {
		public TextView name;

		public ViewHolder(TextView name) {
			super();
			this.name = name;
		}
	}

	public String getCurrentPlayUrl() {
		return currentPlayUrl;
	}

	public void setCurrentPlayUrl(String currentPlayUrl) {
		String cid = FoneUtil.getCIdByUrl(currentPlayUrl);
		if (!TextUtils.isEmpty(cid) && !cid.equals(this.cId)) {
			this.currentPlayUrl = currentPlayUrl;
			this.cId = cid;
			notifyDataSetChanged();
		}
	}

	public void setData(List<Cnt> cntList) {
		this.cntList = cntList;
		notifyDataSetChanged();
	}

	public String getcId() {
		return cId;
	}

}
